package com.youlu.campus.admin.leads.service.impl;

import com.alibaba.fastjson.JSON;
import com.mongodb.client.result.UpdateResult;
import com.youlu.campus.admin.base.BaseService;
import com.youlu.campus.admin.leads.dto.SalesLeadsRequst;
import com.youlu.campus.admin.leads.dto.SalesLeadsResponse;
import com.youlu.campus.admin.leads.entity.SalesLeads;
import com.youlu.campus.admin.leads.service.SalesLeadsService;

import com.youlu.campus.base.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SalesLeadsServiceImpl extends BaseService implements SalesLeadsService {
    @Override
    public Page<SalesLeadsResponse> list(SalesLeadsRequst requst) {
        log.info(":>>> 开始获取学习计划:{}", JSON.toJSONString(requst));
        if (Objects.isNull(requst)) {
            log.error(":>>> 获取学习计划列表参数错误");
            throw new BusinessException("参数错误!");
        }
        Query query = new Query();
        query.addCriteria(Criteria.where("deleted").ne(Boolean.TRUE));
        if (StringUtils.isNotBlank(requst.getClassification())) {
            query.addCriteria(Criteria.where("classification").is(requst.getClassification()));
        }
        if (StringUtils.isNotBlank(requst.getTeacherId())) {
            query.addCriteria(Criteria.where("teacherId").is(requst.getTeacherId()));
        }
        if (StringUtils.isNotBlank(requst.getMobile())) {
            Pattern pattern = Pattern.compile("^.*" + requst.getMobile() + ".*$", Pattern.CASE_INSENSITIVE);
            query.addCriteria(Criteria.where("mobile").regex(pattern));
        }

        if (Objects.nonNull(requst.getStartTime()) && Objects.nonNull(requst.getEndTime())) {
            query.addCriteria(Criteria.where("createdTime").lte(requst.getEndTime()).gte(requst.getStartTime()));
        } else if (Objects.nonNull(requst.getStartTime())) {
            query.addCriteria(Criteria.where("createdTime").gte(requst.getStartTime()));
        } else if (Objects.nonNull(requst.getEndTime())) {
            query.addCriteria(Criteria.where("createdTime").lte(requst.getEndTime()));
        }
        long count = mongoTemplate.count(query, SalesLeads.class);
        PageRequest pageRequest = PageRequest.of(requst.getPage() - 1, requst.getPageSize());
//        query.with(new Sort(Sort.Direction.DESC, "createdTime"));
        Sort sort=Sort.by(Sort.Direction.DESC, "createdTime");
        query.with(sort);
        List<SalesLeads> datas = mongoTemplate.find(query.with(pageRequest), SalesLeads.class);
        List<SalesLeadsResponse> list = datas.stream().map(articleInfo -> {
            SalesLeadsResponse response = mapperFacade.map(articleInfo, SalesLeadsResponse.class);
            return response;
        }).collect(Collectors.toList());
        return PageableExecutionUtils.getPage(list, pageRequest, () -> count);
    }

    @Override
    public boolean update(SalesLeads salesLeads) {
        log.info(":>>> 开始修改学习计划:{}", JSON.toJSONString(salesLeads));
        if (Objects.isNull(salesLeads) || StringUtils.isBlank(salesLeads.getId())) {
            log.error(":>>> 修改学习计划失败,参数错误");
            throw new BusinessException("参数错误!");
        }
        Query query = new Query().addCriteria(Criteria.where("id").is(salesLeads.getId()));
        Update update = new Update();
        if (StringUtils.isNotBlank(salesLeads.getComment())) {
            update.set("comment", salesLeads.getComment());
            update.set("updatedTime", new Date());
        }
        UpdateResult result = mongoTemplate.updateFirst(query, update, SalesLeads.class);
        return result.getModifiedCount() > 0 ? true : false;
    }
}
